package com.wbcs.system.format;

import java.text.DecimalFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FormatTools
{
    private static Log log=LogFactory.getLog(FormatTools.class);

    public static String substring(String str,Integer start,Integer length)
    {
        return substring(str,start.intValue(),length.intValue());
    }

    public static String substring(String str,int start,int length)
    {
        if(str!=null)
        {
            str=str.trim();
            if(length>0)
            {
                if(str.length()>start+length)
                {
                    str=str.substring(start,length);
                }
            }else
            {
                if(str.length()>start)
                {
                    str=str.substring(start);
                }
            }
        }
        return str;
    }

    public static String formatDouble(String srcString,String pattern)
    {
        try
        {
            if(srcString==null||srcString.trim().equals(""))
            {
                return "";
            }
            DecimalFormat df=new DecimalFormat(pattern);
            srcString=df.format(Double.parseDouble(srcString));
            return srcString;
        }catch(Exception e)
        {
            log.error("以"+pattern+"格式格式化"+srcString+"时，发生了异常：",e);
            return srcString;
        }
    }

    public static String formatLong(String srcString,String pattern)
    {
        try
        {
            if(srcString==null||srcString.trim().equals(""))
            {
                return "";
            }
            DecimalFormat df=new DecimalFormat(pattern);
            srcString=df.format(Long.parseLong(srcString));
            return srcString;
        }catch(Exception e)
        {
            log.error("以"+pattern+"格式格式化"+srcString+"时，发生了异常：",e);
            return srcString;
        }
    }

    //    /**
    
    
    
    
    
    
    
    //     */
    
    
    //        try
    
    
    
    
    
    
    
    
    
    //        }catch(Exception e)
    
    
    
    
    
    public static String htmlEncode(String src)
    {
        if(src==null||src.trim().equals(""))
        {
            return src;
        }
        StringBuffer result=new StringBuffer();
        char character;
        for(int i=0;i<src.length();i++)
        {
            character=src.charAt(i);
            if(character=='<')
            {
                result.append("&lt;");
            }else if(character=='>')
            {
                result.append("&gt;");
            }else if(character=='\"')
            {
                result.append("&quot;");
            }else if(character=='\'')
            {
                result.append("&#039;");
            }else if(character=='\\')
            {
                result.append("&#092;");
            }else
            {
                result.append(character);
            }
        }
        return result.toString();
    }
}
